/**
 * $Id: editor_plugin_src.js $
 *
 * @author Richard Davies
 */

(function() {
	tinymce.create('tinymce.plugins.nbspFixPlugin', {
	
		/**
		 * Flag to track if content has already been 'fixed'. The function will get called many
		 * times, but it only needs to fix the nbsp entities on the first run.
		 */
		fixed : false,

		init : function(ed, url) {
			// Modify the <textarea> HTML loaded into the editor to preserve &nbsp; entities 
			ed.onBeforeSetContent.add(function(ed, o) {
				if (!this.fixed && tinymce.isGecko && !tinymce.isSafari) {
					var textarea = document.getElementById(ed.id);
					textarea.innerHTML = textarea.innerHTML.replace(/&nbsp;/gi, "&amp;nbsp;");
					if (textarea.defaultValue)	// undefined when switching to fullscreen view
						o.content = textarea.defaultValue;
				}
				this.fixed = true;
			});
		},
		
		/**
		 * Returns information about the plugin as a name/value array.
		 * The current keys are longname, author, authorurl, infourl and version.
		 *
		 * @return {Object} Name/value array containing information about the plugin.
		 */
		getInfo : function() {
			return {
				longname : '&nbsp fix',
				author : 'Richard Davies',
				authorurl : 'http://www.richarddavies.us',
				infourl : 'http://sourceforge.net/tracker/index.php?func=detail&aid=1796699&group_id=103281&atid=738747',
				version : '3.2'
			};
		}
	
	});
	
	// Register plugin
	tinymce.PluginManager.add("nbspfix", tinymce.plugins.nbspFixPlugin);
}) ();


